package cn.edu.sdjzu.controller;

import cn.edu.sdjzu.service.CalculatorService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Autowired
    private CalculatorService calculatorService;

    @GetMapping("/gateway/add")
    @SentinelResource(value = "gatewayAdd", blockHandler = "handleBlock", fallback = "handleFallback")
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        return calculatorService.add(a, b);
    }

    // 流控降级处理
    public Integer handleBlock(Integer a, Integer b, BlockException ex) {
        return -1; // 返回流控提示
    }

    // 异常降级处理
    public Integer handleFallback(Integer a, Integer b, Throwable ex) {
        return -2; // 返回异常提示
    }
}
